package com.ncp.model.biz.news;

import java.sql.Connection;
import com.ncp.model.dao.*;
import com.ncp.model.entity.news.NewsBean;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class NewsManager {
	private Connection ct=null;
	private PreparedStatement ps=null;
	private ResultSet rs=null;
	private int pageCount=0;
	private int rowCount=0;
	public int getPageCount() {
		return pageCount;
	}
	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}
	public int getRowCount() {
		return rowCount;
	}
	public void setRowCount(int rowCount) {
		this.rowCount = rowCount;
	}
	//放在首页展示的新闻
	public ArrayList showNews(){
		ArrayList al=new ArrayList();
		try {
			ct = DbConnection.getConnByDataSource();
			String sql="select * from NEWS where picture!='null'";
			ps=ct.prepareStatement(sql);
			rs=ps.executeQuery();
			while(rs.next()){
				NewsBean ab=new NewsBean();
				ab.setCont(rs.getString(4));
				ab.setId(rs.getInt(1));
				ab.setTitle(rs.getString(2));
				//setPdate(rs.getTimestamp("pdate"));
				ab.setpDate(rs.getTimestamp(3));
				ab.setPicture(rs.getString(5));
				ab.setViews(rs.getInt(6));
				al.add(ab);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			this.closeResource();
		}
		return al;
	}
	//切割函数
	public static String[] split(String str, String sign) {
	  String[] strData = null;
	  StringTokenizer st1 = new StringTokenizer(str, sign);
	  strData = new String[st1.countTokens()];
	  int i = 0;
	  while (st1.hasMoreTokens()) {
	   strData[i] = st1.nextToken().trim();
	   i++;
	  }
		  return strData;
	}
	//添加新闻
	public boolean addArticle(String newsTitle,String newsCont,String picture){
		boolean b=false;
		try {
			ct = DbConnection.getConnByDataSource();
			String sql="insert into NEWS values(null,'"+newsTitle+"',now(),'"+newsCont+"','"+picture+"',0)";
			//System.out.println(sql);
			ps=ct.prepareStatement(sql);
			ps.executeUpdate();
			b=true;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			this.closeResource();
		}
		return b;
	}
	//得到所有新闻
	public ArrayList getAllNews(){
		ArrayList al=new ArrayList();
		try {
			ct = DbConnection.getConnByDataSource();
			String sql="select * from NEWS";
			ps=ct.prepareStatement(sql);
			rs=ps.executeQuery();
			while(rs.next()){
				NewsBean nb=new NewsBean();
				nb.setCont(rs.getString(4));
				nb.setId(rs.getInt(1));
				nb.setTitle(rs.getString(2));
				nb.setpDate(rs.getTimestamp(3));
				nb.setPicture(rs.getString(5));
				nb.setViews(rs.getInt(6));
				al.add(nb);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			this.closeResource();
		}
		return al;
	}
	//取出一条新闻
	public NewsBean getNews(String id){
		NewsBean nb=new NewsBean();
		int views = 0;
		try {
			ct = DbConnection.getConnByDataSource();
			String sql="select * from NEWS where NEWSID="+id;
			ps=ct.prepareStatement(sql);
			rs=ps.executeQuery();
			if(rs.next()){
				nb.setCont(rs.getString(4));
				nb.setId(rs.getInt(1));
				nb.setTitle(rs.getString(2));
				nb.setpDate(rs.getTimestamp(3));
				nb.setPicture(rs.getString(5));
				views=rs.getInt(6);
				nb.setViews(views);
				this.viewsAdd(id, views);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			this.closeResource();
		}
		return nb;
	}
	//增加浏览次数
	public void viewsAdd(String id,int views){
		try {
			ct = DbConnection.getConnByDataSource();
			views++;
			String sql="update NEWS set views="+views+" where NEWSID="+id;
			ps=ct.prepareStatement(sql);
			ps.executeUpdate();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			this.closeResource();
		}
	}
	//删除新闻
	public boolean delNews(String id){
		Boolean b=false;
		try{
			ct = DbConnection.getConnByDataSource();
			String sql="delete from NEWS where NEWSID='"+id+"'";
			ps=ct.prepareStatement(sql);
			int num=ps.executeUpdate();
			if(num==1){
				b=true;
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try{
				if(ps!=null){ps.close();}
				if(ct!=null){ct.close();}
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		return b;
	}
	//分页
	public ArrayList fenYe(int pageSize,int pageNow){
		ArrayList al=new ArrayList();
  		try {
  			ct = DbConnection.getConnByDataSource();
  			ps=ct.prepareStatement("select count(*) from NEWS");
			rs=ps.executeQuery();
			if(rs.next()){
				rowCount=rs.getInt(1);	
			}
			if(rowCount%pageSize==0){
				pageCount=rowCount/pageSize;
			}else{
				pageCount=rowCount/pageSize+1;
			}
  			if(rs.next()){
  				rowCount=rs.getInt(1);
  			}
  			if(rowCount%pageSize==0){
  				pageCount=rowCount/pageSize;
  			}else{
  				pageCount=rowCount/pageSize+1;
  			}
  			ps=ct.prepareStatement("select * from NEWS LIMIT "+(pageNow-1)*pageSize+","+pageSize+"");
  			rs=ps.executeQuery();
  			while(rs.next()){
				NewsBean ab=new NewsBean();
				ab.setCont(rs.getString(4));
				ab.setId(rs.getInt(1));
				ab.setTitle(rs.getString(2));
				ab.setpDate(rs.getTimestamp(3));
				ab.setPicture(rs.getString(5));
				al.add(ab);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			this.closeResource();
		}
		return al;
	}
	
	//关闭资源
	public void closeResource(){
		try {
			if(rs!=null){
				rs.close();
				rs=null;
			}
			if(ps!=null){
				ps.close();
				ps=null;
			}
			if(ct!=null){
				ct.close();
				ct=null;
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
}
